//localStorage.clear();
let questionData = JSON.parse(localStorage.getItem("questionData"));
var check = document.getElementsByName("check");
//删除按钮
function deltr(obj, uuid) {
    layer.open({
        btnAlign: 'c',
        skin: 'demo-class', //皮肤
        area: ['250px', '130px'],
        title: '确定要删除这份问卷吗', //指定弹窗标题
        // offset:'0',
        btn: ['确定', '取消'], //弹框的确定取消
        btn1: function (index) {
            var tr = obj.parentNode.parentNode;
            tr.parentNode.removeChild(tr);
            layer.close(index);
            //删除当前问卷判断复选框是否全选
            change();
            delchange();
            delqu(uuid);

        },
        btn2: function (index) {
            layer.close(index);
        }

    });



}

function delchange() {
    // //无问卷时跳转页面
    if (check.length == 1) {
        self.location = 'newQuestion.html';
    }
}

// 删除问卷
function delqu(uuid) {
    delete questionData[uuid];
    localStorage.setItem("questionData", JSON.stringify(questionData))
}

//全选按钮
function quan(ch) {
    let sda = ch.checked;
    let zhe = document.getElementsByName("check");
    for (var i in zhe) {
        zhe[i].checked = sda;

    }
}
var check = document.getElementsByName("check");
// console.log(check);
var checklength = check.length;

//全删按钮
function alldel() {
    if (check.length > 1) {


        layer.open({
            btnAlign: 'c',
            skin: 'demo-class', //皮肤
            area: ['250px', '130px'],
            title: '确定要删除选中的问卷吗', //指定弹窗标题

            btn: ['确定', '取消'], //弹框的确定取消
            btn1: function (index) {
                for (var i = 0; i < check.length - 1; i++) {
                    let checkdate = check[i].checked;
                    //删除操作
                    if (checkdate == true) {
                        var td = check[i].parentNode.parentNode;
                        td.parentNode.removeChild(td);

                        i--;
                    }
                }
                layer.close(index);
                //无问卷时跳转页面
                if (check.length == 1) {
                    self.location = 'newQuestion.html';
                }
            },
            btn2: function (index) {
                layer.close(index);
            }
        });

    }
}


// //全选按钮的逻辑,普通复选框选中，判断是否全选
function ifquan() {
    for (var i = 0; i < check.length; i++) {
        if (!check[i].checked) {
            check[check.length - 1].checked = false;
            break;
        } else if (i == check.length - 2) {
            console.log("全都选中了");

            check[check.length - 1].checked = 1;
        }
    }

}
//某个普通复选框删除后判断是否全选
function change() {

    for (var i = 0; i < check.length; i++) {
        if (!check[i].checked) {
            check[check.length - 1].checked = false;
            break;
        } else if (i == check.length - 2) {
            console.log("全都选中了");

            check[check.length - 1].checked = 1;
        }
    }

}
document.getElementsByName("statementall");

//填写问卷按钮
function writethis(uuid) {
    window.location = "questionWrite.html?uuid=" + uuid;

}
//详情页面
function detailthis(uuid) {
    window.location = "data.html?uuid=" + uuid;
}
// 编辑页面
function editthis(uuid) {
    // console.log(questionData)
    // console.log(uuid)
    // console.log(questionData[uuid].state);
    questionData[uuid].state = 0
    // console.log(questionData);
    localStorage.setItem("questionData", JSON.stringify(questionData))
    location.href = "index.html"
    
}
function xiugaibianji(uuid) {
    let temp = document.getElementById('tiaozhuan')
    temp.setAttribute("onclick", "editthis(\'"+uuid+"\')")
    location.href = "#bianji"
}

//json数据插入html
var reloadJson = function () {

    //let table = $("#table")[0]; =
    let masterTable = $("#table"); //要取这个

    console.log(questionData);
    if (questionData == null) {
        self.location = 'newQuestion.html';
    }

    for (var key in questionData) {
        var questionTitle = questionData[key].questionTitle;
        var date = questionData[key].endTime;
        var statement = questionData[key].state;
        //根据时间是否截至将状态改为完成
        var nowtime1 = Date.parse(new Date());
        var endtime1 = questionData[key].endTime;
        var endtime2 = new Date(endtime1);
        var nowtime2 = new Date(nowtime1);
        var statement = questionData[key].state;
        if (endtime2.getTime() <= nowtime2.getTime()) {
            statement = "1";
            questionData[key].state = "1";
        }
        // console.log(endtime2);
        //判断状态
        if (statement == "0") {
            statement = "发布中";

        } else if (statement == "1") {
            statement = "已结束";

        } else {
            statement = "未发布";

        }


        //html代码
        let masterTableHtml = "<tr><th><input type='checkbox' name='check' class='check' onchange ='ifquan()'  /></th>" +
            "<th>" + questionTitle + "</th>" +
            "<th>" + date + "</th>" +
            "<th>" + statement + "</th>" +
            "<th><button class='layui-btn layui-btn-primary layui-btn-sm' name='edit' onclick=\"xiugaibianji(\'" + key + "\')\">编辑问卷状态</button>" +
            "<button class='layui-btn layui-btn-primary layui-btn-sm' onclick=\"deltr(this, \'" + key + "\')\" id='del' name='del' >删除</button>" +
            "<button class='layui-btn layui-btn-primary layui-btn-sm'  name='write' onclick=\"writethis(\'" + key + "\')\">填写问卷</button>" +
            "<button class='layui-btn layui-btn-primary layui-btn-sm' name='detail' id='detail' onclick=\"detailthis(\'" + key + "\')\">查看数据</button></th></tr>";


        masterTable.append(masterTableHtml);

    }
    //获取查看数据的name
    var detailall = document.getElementsByName("detail");
    var editall = document.getElementsByName("edit");
    var writeall = document.getElementsByName("write");

    var i = 0;
    //根据statement状态 将按钮禁用
    for (var key in questionData) {

        // 当状态为0即发布中时，禁用编辑按钮
        if (questionData[key].state == "0") {
            document.getElementsByName("edit")[i].setAttribute("disabled", true);
            editall[i].style.backgroundColor = "#D3D3D3";

        }
        if (questionData[key].state == "1") {
            //当状态为1即已结束时，禁用编辑和填写问卷按钮
            document.getElementsByName("edit")[i].setAttribute("disabled", true);
            document.getElementsByName("write")[i].setAttribute("disabled", true);
            writeall[i].style.backgroundColor = "#D3D3D3";
            editall[i].style.backgroundColor = "#D3D3D3";
        } else if (questionData[key].state == "2") {
            //当状态为2即未发布时，禁用查看数据和填写问卷按钮   
            document.getElementsByName("detail")[i].setAttribute("disabled", true);
            document.getElementsByName("write")[i].setAttribute("disabled", true);
            detailall[i].style.backgroundColor = "#D3D3D3";
            writeall[i].style.backgroundColor = "#D3D3D3";
        }
        i++;
    }



}
window.onload = function () {
    reloadJson();
}